/**
 * TB_DOOR_ZONE
 * 
 *
 *<pre> 
 * 开发公司：苏州东大金点信息技术有限公司
 * 开发人员：唐红锋
 * 邮箱地址：55872655@qq.com
 * 创建时间：2019-10-23 18:09:08
 *</pre>
 */
$(function() {
	zone  = new Zone();
	zone.init();	
	formUrl = zone.formUrl;
});

(function() {
	//定义常量
	var 	_consts = {
			TREE_ID : 'zoneTree',// 树
			TREE : '#zoneTree', //树ID
			FORM : '#zoneForm',// 表单form
			FORMGET : '#zoneFormGet'// 表单form
			
	};
	/**
	 * TB_DOOR_ZONE 对象
	 * @returns {Zone}
	 */
	Zone = function() {
		//定义属性
	};

	/**
	 * 方法
	 */
	Zone.prototype = {
		consts:	_consts,
		/**
		 * 初始化
		 */
		init : function() {
			if (this.hasInit) // 是否已初始化
				return false;
			this.hasInit = true;
			if($(this.consts.TREE).length > 0){
				this._initZoneTree();
			}
			if ($(this.consts.FORM).length > 0){//表单
				this._initForm();
				this._initData();
				this._initOffice('e');
			}
			if ($(this.consts.FORMGET).length > 0){// 明细页面office控件初始化
				this._initOffice('r');
			}
		},
		_initOffice : function(_rights){
		},

		_initZoneTree:function(){
			var me = this;
			//初始化布局
			var layout =   $('body').layout({ applyDefaultStyles: true});  
			layout.addPinBtn( ".pinBtn", "west" );
			// 树
			this._treeFrameResize();
			// 缩放时候计算高度
			$(window).resize(function(){  
				me._treeFrameResize();
			});
			this.loadTree();
	        //初始化滚动
	        this.initLeftScroll();
	        //初始化树的顶部按钮
			this.initTreeToolbar();
		},
		/**
		 * 缩放时候计算高度
		 */
		_treeFrameResize:function (){
			$('.treeFrame').height( $(window).height()-20);
			$(this.consts.TREE).height( $(window).height()-140);
		},
		// 加载树
		loadTree:function (){
			var me = this;
			this._treeRootId=0;
			this.expandByDepth = 1;
			var setting = {
				async: {enable: false},
				data: {
					key:{name:"name"},
					simpleData: {
						enable: true,
						idKey: "id",
						pIdKey: "pid",
						rootPId:"0"
					}
				},
				callback:{
					onClick: function(e, treeId, treeNode) {
						me.treeOnLeftClick(me,treeNode);	
					},
					onRightClick :function(e, treeId, treeNode) {
						me.treeOnRightClick(me,treeNode,e);
					},
					beforeDrop: null,
					onDrop: null
				}
			};
			var url=__ctx+"/rdp/area/zone/getTreeData.htm";
			$.post(url,function(result){
				me.zoneTree=$.fn.zTree.init($(me.consts.TREE), setting,result);
		        if(me.expandByDepth!=0) {
		            var nodes = me.zoneTree.getNodesByFilter(function(node){
		                return (node.level==me.expandByDepth);
		            });
		            if(nodes.length>0){
		                for(var idx=0;idx<nodes.length;idx++){
		                	me.zoneTree.expandNode(nodes[idx],true,false);
		                }
		            }
		        } else   {
		        	me.zoneTree.expandAll(true);
		        }
			});
		},
		/**
		 * 重新加载树
		 */
		reFresh:function(){
			//刷新左边的树
			this.loadTree();
		},
		treeOnLeftClick:function(me, treeNode) {
			var nodeId=treeNode.id;
			me.selectTreeNode = treeNode;
			if(nodeId==0 || nodeId == '0')
				return;
			var url=__ctx+"/rdp/area/zone/get.htm?id="+nodeId;
			$("#listFrame").attr("src",url);
		},
		treeOnRightClick:function(me,treeNode,e){
			if (!treeNode) 
				return;
			this.zoneTree.selectNode(treeNode);
			
			//获取当前点击左树的参与者Id
			var nodeId = treeNode.id;
			var menu=null;
			///特殊节点能新增
			if(nodeId==''||nodeId==undefined ||nodeId =="0"){
				menu=$('#rootMenu');
			}
			else{
				menu=$('#groupMenu');
			}
				
			menu.contextMenu(e,{
				onItem: function(context, ev) {
					var target =$(ev.target), 
					action = target.data("action");
				if (target.hasClass('disabled'))
					return false;
				switch (action) {
					case "node_add":// 增加节点
						me.addNode(treeNode);
						break;
					case "node_edit":// 编辑节点
						me.editNode(treeNode);
						break;
					case "node_del":// 删除节点
						me.delNode(treeNode);
						break;
				}
				}
			});
		},
		delNode:function(node){
			if(node.children != null && node.children.length > 0) {
				DialogUtil.toastr("请先删除子节点！");
				return;
			}
			
			var me = this;
			var url =__ctx+"/rdp/area/zone/remove.htm",me =this;
			DialogUtil.confirm('确认删除吗？',function(rtn){
				if(!rtn)
					return;
				
				$.ajax({
					type : "POST",
					url : url,
					data : {id:node.id},
					success : function(responseText) {
						var resultMessage = new com.lc.form.ResultMessage(responseText);
					    if (resultMessage.isSuccess()) {
					    	me.reFresh();
					    	DialogUtil.toastr(resultMessage.getMessage(),true);
					    } else{
					    	DialogUtil.error(resultMessage.getMessage());
					    }
					},
					errror:function(error){
						DialogUtil.error(error);
					}
				});
			});
		},
		addNode:function(node){
			var url=__ctx+"/rdp/area/zone/edit.htm?pid="+node.id;
			$("#listFrame").attr("src",url);
		},
		editNode:function(node){
			var url=__ctx+"/rdp/area/zone/edit.htm?id="+node.id+"&pid="+node.getParentNode().id;
			$("#listFrame").attr("src",url);
		},
		/**
		 * 左侧菜单的滚动
		 */
		initLeftScroll:function(){
	    	$(_consts.TREE).niceScroll({
	    		horizrailenabled : false,
	    		cursorborder : "0",
	    		cursorwidth : "6px",
	    		cursorcolor : "#2A2A2A",
	    		zindex : "5555",
	    		autohidemode : true,
	    		bouncescroll : true,
	    		mousescrollstep : '40',
	    		scrollspeed : '100',
	    		background : "#999",
	    		cursoropacitymax : "0.6",
	    		cursorborderradius : "0"
	    	});
	    	$(_consts.TREE).getNiceScroll().resize();
		},
		initTreeToolbar:function(){
			var me = this;
			$('.tree-toolbar').on("click", "a.btn", function(){
				if($(this).hasClass("fa-refresh")){//刷新
					me.loadTree();
				}else	if($(this).hasClass("fa-expand")){//展开
					me.zoneTree.expandAll(true);
				} else{
					me.zoneTree.expandAll(false);	
				}
			});
		},
		blank : function(){
			$("#listFrame").attr("src",'');
		},
		
		/**
		 * 初始化表单
		 */
		_initForm : function() {
			var me = this, form = $(this.consts.FORM), frm = form.form();
			me.formUrl = new com.lc.form.FormData(form);
			// 触发表单验证
			frm.valid();
			// 处理表单保存
			$(document).on('click', 'a.fa-save', function() {
				var $el = $(this);
				$el.button('loading');
				// office提交
        		OfficePlugin.submit();
				me.formUrl.submit(me._showResponse, $el);
			});
		},
		/**
		 * 初始化数据
		 */
		_initData : function(){
			if(!$.isEmpty(frameElement) 
				&& !$.isEmpty(frameElement.dialog) 
				&& !$.isEmpty(frameElement.dialog.params)
				&& !$.isEmpty(frameElement.dialog.params.data)){
				var data = frameElement.dialog.params.data;
				this.formUrl.setData("[name^='m:']", data);
				AttachementControl.initDataTo($("div[name='div_attachment_container']")); //附件初始化
			}else{
				$("[name^='m:']", $(_consts.FORM)).each(function(){
					var data = $(this);
	        		// 单选框特殊处理 
	            	if(data.is('input') && "radio" == data.attr("type")){
			        		var defaultVal = data.attr("defaultVal");
			        		var defaultValue = data.attr("defaultValue");
		            		if(defaultValue=="true"&&defaultVal=="true"){
		            			data.prop("checked", "checked");
		            		}
		        	}
		        });
			}
			
			this.formUrl.validate();
		},
		/**
		 * 表单成功返回信息
		 * 
		 * @param responseText
		 */
		_showResponse : function(responseText) {
			var msg = new com.lc.form.ResultMessage(responseText);
			if (msg.isSuccess()) {
				if(parent.zone)parent.zone.reFresh();
				
				DialogUtil.confirm(msg.getMessage() + ',是否继续操作',
					function(rtn) {
					if(rtn)
						window.location.reload(true);
					else
				    	if(parent.zone)parent.zone.blank();
					});
		    } else{
		    	DialogUtil.error(msg.getMessage());
		    }
		}
	};
})();